<template>
  <div>
    <div class="search-context">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="addMenu">新增</el-button>
      </div>
    </div>
    <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="40%"
        :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-form-item label="上级菜单">
            <el-cascader :options="menuTreeRoot" :show-all-levels="false" v-model="form.parentId" :props="optionProps"
                         @change="selectParentMenu">
            </el-cascader>
          </el-form-item>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="菜单名称">
              <el-input v-model="form.menuName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单别名">
              <el-input v-model="form.menuAlias"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="图标">
          <el-popover
              placement="bottom-start"
              width="450"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
          >
            <IconSelect ref="iconSelect" @selected="selected"/>
            <el-input slot="reference" v-model="form.menuIcon" style="width: 450px;" placeholder="点击选择图标" readonly>
              <svg-icon v-if="form.menuIcon" slot="prefix" :icon-class="form.menuIcon" class="el-input__icon"
                        style="height: 32px;width: 16px;"/>
              <i v-else slot="prefix" class="el-icon-search el-input__icon"/>
            </el-input>
          </el-popover>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜单类型">
              <el-select v-model="form.menuType" placeholder="请选择菜单类型">
                <el-option :key="type.value" :value="type.value" :label="type.label" v-for="type in menuTypeOptions"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单级别">
              <el-select v-model="form.menuLevel" placeholder="请选择菜单级别">
                <el-option :key="level.value" :value="level.value" :label="level.label"
                           v-for="level in menuLevelOptions"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="序号">
              <el-input-number v-model="form.sort" :min="1" :max="10" label="描述文字"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单状态">
              <el-switch
                  v-model="form.menuStatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限标识">
              <el-input v-model="form.menuAuth"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路由地址">
              <el-input v-model="form.menuRouterPath"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请求地址">
              <el-input v-model="form.menuPath"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求方法">
              <el-select v-model="form.method" placeholder="请选择请求方法">
                <el-option :key="level.value" :value="level.value" :label="level.label"
                           v-for="level in methodOptions"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlerAddMenu">确 定</el-button>
      </span>
    </el-dialog>
    <div style="margin-top: 20px">
      <el-table
          :data="tableData"
          style="width: 100%"
          row-key="menuId"
          highlight-current-row
          :indent="20"
          @current-change="handleCurrentChange"
          border
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            prop="parentId"
            v-if="false"
            label="上级菜单Id"
            width="180">
        </el-table-column>
        <el-table-column
            prop="menuName"
            label="菜单名"
            width="250">
        </el-table-column>
        <el-table-column
            prop="menuIcon"
            label="图标"
            align="center"
            width="60">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.menuIcon ? scope.row.menuIcon : ''"/>
          </template>
        </el-table-column>
        <el-table-column
            prop="sort"
            label="序号"
            align="center"
            width="80">
        </el-table-column>
        <el-table-column
            prop="menuLevel"
            label="菜单级别"
            align="center"
            width="110"
            :formatter="menuLevelFormatter"
        >
        </el-table-column>
        <el-table-column
            prop="menuStatus"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.menuStatus"
                active-color="#13ce66"
                inactive-color="#ff4949" disabled>
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
            prop="menuPath"
            label="路由地址">
        </el-table-column>
        <el-table-column
            prop="menuRouterPath"
            label="路由地址">
        </el-table-column>
        <el-table-column
            prop="method"
            label="请求方法">
        </el-table-column>
        <el-table-column
            prop="menuAuth"
            label="权限标识"
            width="150">
        </el-table-column>
        <el-table-column
            prop="createDateTime"
            label="创建时间">
        </el-table-column>
        <el-table-column
            label="操作"
            width="180"
            align="center"
        >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="editMenu(scope.row)"
                       size="mini"></el-button>
            <el-popover width="160" :ref="`popover-${scope.$index}`" placement="top">
              <p>确定删除该项吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button type="text" size="mini" @click="deleteHandleClose(scope.$index)">
                  取消
                </el-button>
                <el-button type="danger" size="mini" @click="removeMenu(scope.row,scope.$index)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" icon="el-icon-delete" circle size="mini"
                         style="margin-left: 10px"></el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import IconSelect from '@/components/IconSelect'

export default {
  name: "resource",
  components: {IconSelect,},
  data() {
    return {
      visible: false,
      rootMenuId: '0',
      searchDeptName: '',
      methodOptions: [
        {value: 'GET', label: 'GET'},
        {value: 'POST', label: 'POST'},
        {value: 'PUT', label: 'PUT'},
        {value: 'DELETE', label: 'DELETE'}
      ],
      menuTypeOptions: [
        {value: 1, label: '菜单'},
        {value: 2, label: '按钮'},
        {value: 3, label: '链接'},
        {value: 4, label: '关联接口'},
      ],
      menuLevelOptions: [
        {value: 1, label: '一级菜单'},
        {value: 2, label: '二级菜单'},
        {value: 3, label: '三级菜单'},
        {value: 4, label: '四级菜单'},
        {value: -1, label: '功能按钮'},
        {value: -2, label: '关联接口'},
      ],
      optionProps: {
        value: 'menuId',
        label: 'menuName',
        emitPath: false,
        checkStrictly: true
      },
      form: {
        menuId: null,
        parentId: '0',
        parentMenuName: '根栏目',
        menuName: null,
        menuAlias: null,
        menuIcon: null,
        menuType: 1,
        menuLevel: 1,
        sort: 1,
        menuStatus: true,
        description: '',
        menuPath: '',
        menuRouterPath: '',
        menuAuth: '',
        method: null,
      },
      dialogVisible: false,
      dialogTitle: '新增资源',
      menuTreeRoot: [{
        menuId: '0',
        parentId: '0',
        menuName: '根栏目',
        children: []
      }],
      tableData: [],
      currentRow: null
    }
  },
  created() {
    this.handlerMenuTable();
  },
  methods: {
    handlerMenuTable() {
      var param = {};
      this.systemRequest.menuTree(this, param, (res) => {
        if (res.data) {
          this.tableData = res.data;
          this.menuTreeRoot[0].children = [];

          if (this.tableData) {
            for (let i = 0; i < this.tableData.length; i++) {
              this.menuTreeRoot[0].children.push(this.tableData[i])
            }
          }
          console.log(this.menuTreeRoot)
        }
      })
    },
    handleCurrentChange(val) {
      this.currentRow = val;
      console.log(val.menuId)
    },
    handlerQuery() {
      this.handlerMenuTable();
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(() => {
            done();
          }).catch(() => {
      });
    },
    menuLevelFormatter(row) {
      if (row.menuLevel === this.menuLevelOptions[0].value) {
        return this.menuLevelOptions[0].label;
      } else if (row.menuLevel === this.menuLevelOptions[1].value) {
        return this.menuLevelOptions[1].label;
      } else if (row.menuLevel === this.menuLevelOptions[2].value) {
        return this.menuLevelOptions[2].label;
      } else if (row.menuLevel === this.menuLevelOptions[3].value) {
        return this.menuLevelOptions[3].label;
      }else if (row.menuLevel === this.menuLevelOptions[4].value) {
        return this.menuLevelOptions[4].label;
      } else {
        return this.menuLevelOptions[5].label;
      }
    },
    addMenu() {
      this.dialogTitle = '新增菜单';
      this.dialogVisible = true;
      this.form.menuId = null;
      if (this.currentRow) {
        this.form.parentId = this.currentRow.menuId;
      } else {
        this.form.parentId = '0';
      }
      this.form.parentMenuName = null;
      this.form.menuName = null;
      this.form.menuAlias = null;
      this.form.menuIcon = null;
      this.form.menuType = null;
      this.form.menuLevel = null;
      this.form.sort = 1;
      this.form.menuStatus = true;
      this.form.description = null;
      this.form.menuPath = null;
      this.form.menuRouterPath = null;
      this.form.menuAuth = null;
      this.form.method = null;
    },
    editMenu(val) {
      this.dialogTitle = '编辑菜单';
      this.dialogVisible = true;
      this.form.menuId = val.menuId;
      this.form.parentId = val.parentId;
      this.form.parentMenuName = val.parentMenuName;
      this.form.menuName = val.menuName;
      this.form.menuAlias = val.menuAlias;
      this.form.menuIcon = val.menuIcon;
      this.form.menuType = val.menuType;
      this.form.menuLevel = val.menuLevel;
      this.form.sort = val.sort;
      this.form.menuStatus = val.menuStatus;
      this.form.description = val.description;
      this.form.menuPath = val.menuPath;
      this.form.menuRouterPath = val.menuRouterPath;
      this.form.menuAuth = val.menuAuth;
      this.form.method = val.method;
    },
    handlerAddMenu() {
      var param = this.form;
      if (this.form.menuId) {
        this.systemRequest.modifyMenu(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("菜单信息已修改");
          this.handlerMenuTable();
        })
      } else {
        this.systemRequest.addMenu(this, param, (res) => {
          console.log(res)
          this.dialogVisible = false;
          this.$message.success("成功添加菜单");
          this.handlerMenuTable();
        })
      }
    },
    removeMenu(val, index) {
      console.log(val)
      var param = {
        menuId: val.menuId
      };
      this.systemRequest.deleteMenu(this, param, (res) => {
        console.log(res)
        this.$message.success("成功删除菜单");
        this.handlerMenuTable();
      })

      this.deleteHandleClose(index);
    },
    deleteHandleClose(index) {
      this.$refs[`popover-${index}`].doClose()
    },
    selectParentMenu(val) {
      console.log(val)
      this.form.parentId = val
    },
    // 选中图标
    selected(name) {
      this.form.menuIcon = name
      console.log(name)
    }
  },
}
</script>

<style scoped>

</style>